package com.yuan.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuan.blog.core.utils.PasswordUtils;
import com.yuan.blog.core.utils.UUIDUtils;
import com.yuan.blog.entity.UserEntity;
import com.yuan.blog.mapper.UserMapper;
import com.yuan.blog.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.Date;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,UserEntity> implements IUserService {

    @Autowired
    private UserMapper mapper;

    @Override
    public void register(UserEntity userEntity) throws Exception {
        // 2.密码加密
        userEntity.setPassword(PasswordUtils.hashPw(userEntity.getPassword()));
        // 3.设置用户的状态
        userEntity.setStatus(1);
        userEntity.setCreateTime(new Date());
        userEntity.setActivationCode(UUIDUtils.getId()); // 激活码
        userEntity.setType(0);
        // 4.入库
//        int save = userDao.insert(userEntity);
        save(userEntity);
    }

    @Override
    public UserEntity queryUserByActivation(String code) throws SQLException {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("activation_code",code);
        return mapper.selectOne(wrapper);
    }

    @Override
    public UserEntity login(UserEntity userEntity) throws SQLException {
        //用户查询条件：状态为2，名字存在
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("username",userEntity.getUsername());
        wrapper.eq("status",2);
        //查找用户
        UserEntity user = mapper.selectOne(wrapper);
        if (user == null) {
            return null;
        }
        //校验密码
        if (!PasswordUtils.check(userEntity.getPassword(), user.getPassword())) {
            return null;
        }
        return user;
    }

    @Override
    public void updateHeaderUrl(Integer id, String filePath){
        UpdateWrapper<UserEntity> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        UserEntity user = new UserEntity();
        user.setHeaderUrl(filePath);
        update(user,wrapper);
    }

    @Override
    public boolean updatePwdById(Integer id, String password) throws SQLException {
        String s = PasswordUtils.hashPw(password);
        UserEntity user = new UserEntity();
        user.setPassword(s);
        user.setId(id);
        return updateById(user);
    }

    @Override
    public UserEntity queryById(Integer userId) throws Exception {
        return mapper.selectById(userId);
    }

    @Override
    public int updateStatus(UserEntity userEntity) throws Exception {
        UpdateWrapper<UserEntity> wrapper = new UpdateWrapper<>();
        userEntity.setStatus(2);
        return this.updateById(userEntity)?1:0;
    }
}
